<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\energy;

use app\common\dao\BaseDao;
use app\common\model\energy\EnergyOperateLogModel;

class EnergyOperateLogDao extends BaseDao
{

    /**
     * @return EnergyOperateLogModel
     */
    protected function getModel(): string
    {
        return EnergyOperateLogModel::class;
    }


    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['energy_id']) && $where['energy_id'] !== '', function ($query) use ($where) {
                $query->where('energy_id', intval($where['energy_id']));
            })
            ->when(isset($where['user_id']) && $where['user_id'] !== '', function ($query) use ($where) {
                $query->where('user_id', intval($where['user_id']));
            })
            ->when(isset($where['request_id']) && $where['request_id'] !== '', function ($query) use ($where) {
                $query->where('request_id', intval($where['request_id']));
            })
            ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
                $query->where('status', intval($where['status']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('remark', '%' . trim($where['keywords']) . '%');
            });
    }

    /**
     * 根据设备 ID 删除操作 日志
     * @param int $energyId
     * @return bool
     *
     * @author Amos
     * 2024-05-08 22:51
     */
    public function removeMeterId(int $energyId)
    {
        return $this->getSearch([
            'energy_id' => $energyId,
        ])->delete();
    }

    /**
     * 根据设备 ID 取消任务
     * @param int $energyId
     *
     * @author Amos
     * 2024-05-08 22:52
     */
    public function cancelMeterId(int $energyId)
    {
        return $this->getSearch([
            'energy_id' => $energyId, 'status' => 1
        ])->update(['status' => 7]);
    }


}
